package com.lxp.base.net.okhttp;


import com.lxp.base.utils.SDCardUtils;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;

/**
 * Created by lxp on 2017/6/4.
 */

public class OkHttpHelper {
    private static OkHttpClient instance;
    private static final long cacheSize = 10 * 1024 * 1024; // 10 MiB
    private static final String cacheDirectoryStr = SDCardUtils.getSDCardPath() + "/yonyou/cache";


    public OkHttpHelper() {
        OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
        clientBuilder.connectTimeout(40, TimeUnit.SECONDS);
        clientBuilder.writeTimeout(40, TimeUnit.SECONDS);
        clientBuilder.readTimeout(40, TimeUnit.SECONDS);
        clientBuilder.cache(initCache());
        instance = clientBuilder.build();
    }

    public static OkHttpClient getInstance() {
        synchronized (OkHttp.class) {
            if (null == instance) {
                new OkHttpHelper();
            }
        }
        return instance;
    }

    private Cache initCache() {
        File cacheDirectory = new File(cacheDirectoryStr);
        return new Cache(cacheDirectory, cacheSize);
    }
}
